<div class="orders-container">
  <h1 class="orders-title">My Orders</h1>
  
  <div class="orders-loading" id="ordersLoading">Loading your orders...</div>
  
  <div class="orders-list" id="ordersList" style="display: none;">
    <!-- Orders will be loaded here -->
  </div>
  
  <div class="no-orders" id="noOrders" style="display: none;">
    <h2>You haven't placed any orders yet</h2>
    <p>Start shopping to make your first order!</p>
    <a href="/products" class="browse-products-btn">Browse Products</a>
  </div>
</div>

<script>
  document.addEventListener('DOMContentLoaded', async () => {
    try {
      const response = await fetch('/api/orders');
      const data = await response.json();
      
      const loadingElement = document.getElementById('ordersLoading');
      const listElement = document.getElementById('ordersList');
      const noOrdersElement = document.getElementById('noOrders');
      
      if (data.success && data.orders && data.orders.length > 0) {
        // Sort orders by date (newest first)
        const sortedOrders = data.orders.sort((a, b) => {
          return new Date(b.created_at) - new Date(a.created_at);
        });
        
        sortedOrders.forEach(order => {
          const orderElement = document.createElement('div');
          orderElement.className = 'order-card';
          orderElement.id = `my_order_${order.id}`;
          
          const date = new Date(order.created_at).toLocaleDateString();
          
          orderElement.innerHTML = `
            <div class="order-header">
              <div class="order-number">Order #${order.id}</div>
              <div class="order-date">${date}</div>
            </div>
            <div class="order-body">
              <div class="order-status">${order.status}</div>
              <div class="order-total">$${order.total_price.toFixed(2)}</div>
            </div>
            <div class="order-footer">
              <a href="/order/${order.id}" class="view-order-btn">View Details</a>
            </div>
          `;
          
          // Make the entire card clickable
          orderElement.addEventListener('click', (e) => {
            // Don't trigger if clicking on the button (to avoid double navigation)
            if (!e.target.closest('.view-order-btn')) {
              window.location.href = `/order/${order.id}`;
            }
          });
          
          listElement.appendChild(orderElement);
        });
        
        loadingElement.style.display = 'none';
        listElement.style.display = 'block';
      } else {
        loadingElement.style.display = 'none';
        noOrdersElement.style.display = 'block';
      }
    } catch (error) {
      console.error('Error loading orders:', error);
      document.getElementById('ordersLoading').style.display = 'none';
      document.getElementById('noOrders').style.display = 'block';
    }
  });
</script>

<style>
  .orders-container {
    max-width: 800px;
    margin: 30px auto;
    padding: 20px;
  }
  
  .orders-title {
    margin-bottom: 20px;
    color: #333;
    border-bottom: 2px solid #f0f0f0;
    padding-bottom: 10px;
  }
  
  .orders-loading {
    text-align: center;
    padding: 30px;
    font-style: italic;
    color: #777;
  }
  
  .orders-list {
    display: flex;
    flex-direction: column;
    gap: 15px;
  }
  
  .order-card {
    background-color: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    cursor: pointer;
  }
  
  .order-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
  }
  
  .order-header {
    background-color: #4a89dc;
    color: white;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .order-number {
    font-weight: bold;
    font-size: 16px;
  }
  
  .order-date {
    font-size: 14px;
  }
  
  .order-body {
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #eee;
  }
  
  .order-status {
    font-weight: bold;
    padding: 5px 10px;
    border-radius: 4px;
    background-color: #f8f9fa;
  }
  
  .order-total {
    font-size: 18px;
    font-weight: bold;
    color: #4a89dc;
  }
  
  .order-footer {
    padding: 15px;
    text-align: right;
  }
  
  .view-order-btn {
    display: inline-block;
    background-color: #4a89dc;
    color: white;
    text-decoration: none;
    padding: 8px 15px;
    border-radius: 4px;
    font-size: 14px;
    transition: background-color 0.3s;
  }
  
  .view-order-btn:hover {
    background-color: #357bd8;
  }
  
  .no-orders {
    text-align: center;
    padding: 40px;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  }
  
  .no-orders h2 {
    color: #777;
    margin-bottom: 10px;
  }
  
  .no-orders p {
    margin-bottom: 20px;
    color: #999;
  }
  
  .browse-products-btn {
    display: inline-block;
    background-color: #4a89dc;
    color: white;
    text-decoration: none;
    padding: 10px 20px;
    border-radius: 4px;
    font-weight: bold;
    transition: background-color 0.3s;
  }
  
  .browse-products-btn:hover {
    background-color: #357bd8;
  }
</style>